var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();

Page({
  data:{
    orderList: [],
    page: 1,
    size: 10,
    loadmoreText: '正在加载更多数据',
    nomoreText: '全部加载完成',
    nomore: false,
    totalPages: 1
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    //中大团队拓展
    wx.setNavigationBarTitle({
      title: "个人中心 - 我的订单"
    });
    wx.showLoading({
      title: '加载中...',
      success: function () {

      }
    });
    this.getOrderList();
  },

  /**
       * 页面上拉触底事件的处理函数
       */
  onReachBottom: function () {
    this.getOrderList()
  },

  getOrderList(){
    let that = this;

    if (that.data.totalPages <= that.data.page - 1) {
      that.setData({
        nomore: true
      })
      return;
    }

    util.request(api.OrderList, {page: that.data.page, size: that.data.size}).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          orderList: that.data.orderList.concat(res.data.data),
          page: res.data.currentPage + 1,
          totalPages: res.data.totalPages
        });
        wx.hideLoading();
      }
    });
  },
  payOrder(event){
      let that = this;
      let orderIndex = event.currentTarget.dataset.orderIndex;
      let order = that.data.orderList[orderIndex];
    util.request(api.PayPrepayId, {
      orderId: order.id || 15
    }).then(function (res) {
      if (res.errno === 0) {
        const payParam = res.data;

        //模板消息参数
        var self = this;
        let method = 'POST';
        
        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.package,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function (res) {
            console.log(res);
            //发送模板消息
            let data1={
                "keyword1": {
                  "value": payParam.orderInfo.order_sn,
                  "color": "#173177"
                },
                "keyword2": {
                  "value": payParam.body,
                  "color": "#173177"
                },
                "keyword3": {
                  "value": payParam.orderInfo.add_time,
                  "color": "#173177"
                },
                "keyword4": {
                  "value": payParam.orderInfo.order_price,
                  "color": "#173177"
                },
                "keyword5": {
                  "value": payParam.orderInfo.full_region,
                  "color": "#173177"
                }
              };

            util.request(api.sendTemplateMsg, {
              openId: app.globalData.openId, formId: event.detail.formId, templateId: 'kc-XnjwLydSo_9k4ZdyBZukdIbBqTv6uD0dCLOMJYMI', data: data1
            }, "POST", 'application/json').then(function (res) {
              if (res.errno === 0) {
                console.log("success");
              }
              else {
                console.log("fail");
              }
            })

            //发送短信
            util.request(api.PaySuccess, { orderSn: payParam.orderInfo.order_sn, phone: payParam.orderInfo.mobile }, "POST", 'application/json')
              .then(function (res) {
                console.log(res);
              });
            util.request(api.OrderQuery, { orderId: payParam.orderInfo.id }).then(function (res) { })
            wx.redirectTo({
              url: '/pages/ucenter/order/order',
            })
          },
          'fail': function (res) {
            console.log(res);
            //发送模板消息
              let data2={
                "keyword1": {
                  "value": payParam.orderInfo.order_sn,
                  "color": "#173177"
                },
                "keyword2": {
                  "value": payParam.orderInfo.add_time,
                  "color": "#173177"
                },
                "keyword3": {
                  "value": payParam.body,
                  "color": "#173177"
                },
                "keyword4": {
                  "value": payParam.orderInfo.order_price,
                  "color": "#173177"
                },
                "keyword5": {
                  "value": "网络等原因",
                  "color": "#173177"
                }
              };

            util.request(api.sendTemplateMsg, {
              openId: app.globalData.openId, formId: event.detail.formId, templateId: 'zfgNS-RQy_oWYHcg7daGgZSg5nHyk4zEcv3h7z4DVLY', data: data2
            }, "POST", 'application/json').then(function (res) {
              if (res.errno === 0) {
                console.log("success");
              }
              else {
                console.log("fail");
              }
            })
            util.request(api.OrderQuery, { orderId: payParam.orderInfo.id }).then(function (res) { })
            wx.redirectTo({
              url: '/pages/ucenter/order/order',
            })
          }
          
        });
      }
    });
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){

  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
    wx.reLaunch({
      url: '/pages/ucenter/index/index'
    })

  }
})